package com.soup.common.utils;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 序列化工具
 *
 * @author zhaoyi
 */
@Slf4j
public final class Serializer {

    private Serializer() {
    }

    /**
     * 序列化对象
     *
     * @param obj 待序列化对象
     * @param <T> 序列化的class泛型
     * @return 序列化后的字节数组
     */
    public static <T> byte[] serialize(T obj) {
        if (obj == null) {
            log.error("serialize obj failure, obj is null!");
            return null;
        }
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protostuff = null;
        try {
            protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } catch (Exception e) {
            log.error("serialize obj[{}] occure error!", obj, e);
        } finally {
            buffer.clear();
        }
        return protostuff;
    }

    /**
     * 反序列化成对象
     *
     * @param paramArrayOfByte 待反序列化的字节数组
     * @param targetClass 反序列化的对象class
     * @param <T> 反序列化的对象泛型
     * @return 反序列化后的对象
     */
    public static <T> T deserialize(byte[] paramArrayOfByte, Class<T> targetClass) {
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
            log.error("deserialize byte array failure, byte array is null!");
            return null;
        }
        T instance = null;
        try {
            instance = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("deserialize byte array occure error!", e);
            return null;
        }
        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema);
        return instance;
    }

    /**
     * 序列化List
     *
     * @param objList 待序列化的List
     * @param <T> List泛型
     * @return 序列化后的字节数组
     */
    public static <T> byte[] serializeList(List<T> objList) {
        if (objList == null || objList.isEmpty()) {
            log.error("serialize objList failure, objList is null!");
            return null;
        }

        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objList.get(0).getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protostuff = null;
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer);
            protostuff = bos.toByteArray();
        } catch (IOException e) {
            log.error("serialize objList[{}] occure error!", objList, e);
        } finally {
            buffer.clear();
        }

        return protostuff;
    }

    /**
     * 反序列化成List
     *
     * @param paramArrayOfByte 待反序列化的字节数组
     * @param targetClass 反序列化的List元素对象的class
     * @param <T> 反序列化的对象泛型
     * @return 反序列化后的List
     */
    public static <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) {
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
            log.error("deserialize byte array to List failure, byte array is null!");
            return null;
        }

        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        List<T> result = null;
        try {
            result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
        } catch (IOException e) {
            log.error("deserialize byte array to List occure error!", e);
        }

        return result;
    }

}
